package com.green.business.service.impl;

import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.green.business.domain.Goods;
import com.green.business.domain.Inport;
import com.green.business.domain.Sales;
import com.green.business.domain.Scrap;
import com.green.business.mapper.GoodsMapper;
import com.green.business.mapper.InportMapper;
import com.green.business.mapper.SalesMapper;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import com.green.business.mapper.ScrapMapper;
import com.green.business.service.PALStatementService;
import com.green.business.vo.ReportsVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.DelegatingServerHttpResponse;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PALStatementServiceImpl implements PALStatementService {
    @Autowired
    private InportMapper inportMapper;

    @Autowired
    private SalesMapper salesMapper;
    @Autowired
    private ScrapMapper scrapMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    private Double sum_inport;
    private Double sum_sales;
    private Double pprofitsum;
    private Double nprofitsum;
    //判断list中的元素是否完全相同
    public static boolean name(List<String> list) {
    Set s = new HashSet(list);
    if (s.size()==1) {
        return true;
    }else {
        return false;
    }
}
    //自定义map，将年月份相同的支出金额相加
    public class MyHashMap<K> extends HashMap<K, Double> {
        /**
         * 使用HashMap中containsKey判断key是否已经存在
         *
         * @param key
         * @param value
         * @return
         */
        public Double put(K key, Double value) {
            Double newV = value;
            if (containsKey(key)) {
                Double oldV = get(key);
                newV = oldV + newV;
            }
            return super.put(key, newV);
        }
    }
    @Override
    public ReportsVo queryMonthly(ReportsVo reportsVo) {



        QueryWrapper<Scrap> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "scraptime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "scraptime", reportsVo.getEndTime());
        //按时间升序
        queryWrapper.orderByDesc("scraptime");
        List<Scrap> scrapList = this.scrapMapper.selectList(queryWrapper);
        List<String> months1 = new ArrayList<String>();//支出
        List<String> months11 = new ArrayList<String>();//支出
        List<String> months2 = new ArrayList<String>();//收入
        List<String> months22 = new ArrayList<String>();//收入
        MyHashMap<String> countoutlay = new MyHashMap<>();
//判断是否是在同一月份
        for (Scrap record : scrapList) {
            if (null != record.getGoodsid()) {
                //截取时间段(xxxx年-xx月)yyyy-MM
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String time = sdf.format(record.getScraptime()).substring(0, 7);
                months1.add(time);
    }
}
        //按日
            if(name(months1)) {
                for (Scrap record : scrapList) {
                    if (null != record.getGoodsid()) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String time = sdf.format(record.getScraptime()).substring(0,10);
                        QueryWrapper<Inport> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq(record.getGoodsid()!=null,"goodsid",record.getGoodsid());
                        Inport inport=this.inportMapper.selectOne(queryWrapper1);
                        Double sum = 0.0;
                        sum += record.getNumber() * inport.getInportprice();
                        countoutlay.put(time, sum);
                        months11.add(time);
                    }
                }
            }
            //按月
        if(!name(months1)) {
            for (Scrap record : scrapList) {
                if (null != record.getGoodsid()) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                    String time = sdf.format(record.getScraptime()).substring(0,7);
                    QueryWrapper<Inport> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq(record.getGoodsid()!=null,"goodsid",record.getGoodsid());
                    Inport inport=this.inportMapper.selectOne(queryWrapper1);
                    Double sum = 0.0;
                    sum += record.getNumber() * inport.getInportprice();
                    countoutlay.put(time, sum);
                    months11.add(time);
                }
            }
        }



        QueryWrapper<Sales> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        //按时间升序
        queryWrapper.orderByDesc("salestime");
        List<Sales> salesList = this.salesMapper.selectList(queryWrapper1);
        MyHashMap<String> countrevenue = new MyHashMap<>();
        for (Sales record : salesList) {
            if (null != record.getGoodsid()) {
                //截取时间段(xxxx年-xx月)yyyy-MM
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String time = sdf.format(record.getSalestime()).substring(0, 7);
                months2.add(time);
            }
        }
        //按日
        if(name(months2)) {
            for (Sales record : salesList) {
                if (null != record.getGoodsid()) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String time = sdf.format(record.getSalestime()).substring(0,10);
                    QueryWrapper<Inport> queryWrapper2=new QueryWrapper<>();
                    queryWrapper2.eq(record.getGoodsid()!=null,"goodsid",record.getGoodsid());
                    Inport inport=this.inportMapper.selectOne(queryWrapper2);
                    Double sum = 0.0;
                if (record.getCustomerid()==0) {
                    sum += (record.getNumber() * record.getPrice()-inport.getInportprice());
                }
                else {
                    sum+=(record.getNumber()*record.getMemberprice()-inport.getInportprice());
                }
                countrevenue.put(time, sum);
                months22.add(time);
                }
            }
        }
        //按月
        if(!name(months2)) {
            for (Sales record : salesList) {
                if (null != record.getGoodsid()) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                    String time = sdf.format(record.getSalestime()).substring(0,7);
                    QueryWrapper<Inport> queryWrapper2=new QueryWrapper<>();
                    queryWrapper2.eq(record.getGoodsid()!=null,"goodsid",record.getGoodsid());
                    Inport inport=this.inportMapper.selectOne(queryWrapper2);
                    Double sum = 0.0;
                    if (record.getCustomerid()==0) {
                        sum += (record.getNumber() * record.getPrice()-inport.getInportprice());
                    }
                    else {
                        sum+=(record.getNumber()*record.getMemberprice()-inport.getInportprice());
                    }
                    countrevenue.put(time, sum);
                    months22.add(time);
                }
                }
            }



/*
* 计算月份内利润
* 判断
* 月份比较:
* 月份相同则收入-支出
* 月份不同
* 判断:不同的月份在支出队列内还是在收入队列内
* 若在支出队列:利润=-支出
* 若在收入队列:利润=收入

* */
        List<String> months=new ArrayList<>();
        TreeMap<String,Double> countprofit=new TreeMap<>();//排序
        List<String> months1_copy=new ArrayList<>(months11);
        List<String> months11_copy=new ArrayList<>(months11);
        List<String> months2_copy=new ArrayList<>(months22);
        //只有收入  月份不同  移除months2中months1的元素
        months22.removeAll(months11);
        boolean flag1=months22.removeAll(months11);
        if (flag1||months22.equals(months2_copy)) {
            for (String str : months22) {
                countprofit.put(str, countrevenue.get(str));
                countoutlay.put(str,0.0);
            }
        }
        months1_copy.removeAll(months2_copy);
        boolean flag2= months1_copy.removeAll(months2_copy);//只有支出 月份不同
        if (flag2||months1_copy.equals(months11_copy)) {
            for (String str : months1_copy) {
                countprofit.put(str, (-countoutlay.get(str)));
                countrevenue.put(str, 0.0);
            }
        }
        months11.removeAll(months1_copy);
        boolean flag3=months11.removeAll(months1_copy);//月份相同
        if (flag3) {
            for (String str : months11) {
                countprofit.put(str, (countrevenue.get(str) - countoutlay.get(str)));
            }
        }

//排序:强转为treemap类型 支出
        Map<String, Double> sortedcountoutlay = new TreeMap<String, Double>(countoutlay);
        List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(sortedcountoutlay.entrySet()); //转换为list
        reportsVo.setMonthlyoutlay(list);
//收入
        Map<String, Double> sortedcountrevenue = new TreeMap<String, Double>(countrevenue);
        List<Map.Entry<String, Double>> list1 = new ArrayList<Map.Entry<String, Double>>(sortedcountrevenue.entrySet()); //转换为list
        reportsVo.setMonthlyrevenue(list1);

        List<Map.Entry<String, Double>> list2 = new ArrayList<>(countprofit.entrySet());
        reportsVo.setMonthlyprofit(list2);

        reportsVo.getMonthlyprofit();
        reportsVo.getMonthlyoutlay();
        reportsVo.getMonthlyrevenue();
        return reportsVo;
    }

    @Override
    public Double queryOutlay(ReportsVo reportsVo) {
        sum_inport = 0.0;
        QueryWrapper<Scrap> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "scraptime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "scraptime", reportsVo.getEndTime());
        List<Scrap> scrapList = this.scrapMapper.selectList(queryWrapper);
        for (Scrap record : scrapList) {
            if (null != record.getId()){
                QueryWrapper<Inport> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq(record.getGoodsid()!=null,"goodsid",record.getGoodsid());
                Inport inport=this.inportMapper.selectOne(queryWrapper1);
                sum_inport += record.getNumber() * inport.getInportprice();
            }
        }
        reportsVo.setOutlay(sum_inport);
        return reportsVo.getOutlay();

    }

    @Override
    public Double queryRevenue(ReportsVo reportsVo) {
        sum_inport = 0.0;
        QueryWrapper<Inport> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "inporttime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "inporttime", reportsVo.getEndTime());
        List<Inport> inportList = this.inportMapper.selectList(queryWrapper);
        for (Inport record : inportList) {
            if (null != record.getId()){
                sum_inport += record.getNumber() * record.getInportprice();
            }
        }
        reportsVo.setInportsum(sum_inport);
        sum_sales=0.0;
        QueryWrapper<Sales> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        List<Sales> salesList = this.salesMapper.selectList(queryWrapper1);
        for (Sales record : salesList) {
            if (null != record.getId()) {
                if (record.getCustomerid()==0) {
                    sum_sales += record.getNumber() * record.getPrice();
                }
                else {
                    sum_sales+=record.getNumber()*record.getMemberprice();
                }
            }

        }
        reportsVo.setSalessum(sum_sales);
        reportsVo.setRevenue(reportsVo.getSalessum()-reportsVo.getInportsum());
        return reportsVo.getRevenue();
    }

    @Override
    public List<Map.Entry<String, Double>> querypprofit(ReportsVo reportsVo) {
        pprofitsum = 0.0;
        MyHashMap<String> pprofit = new MyHashMap<>();
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Scrap> queryWrapper2 = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        queryWrapper2.ge(reportsVo.getStartTime() != null, "scraptime", reportsVo.getStartTime());
        queryWrapper2.le(reportsVo.getEndTime() != null, "scraptime", reportsVo.getEndTime());
        //按时间升序
        queryWrapper.orderByDesc("salestime");
        queryWrapper2.orderByDesc("scraptime");
        List<Scrap> scrapList = this.scrapMapper.selectList(queryWrapper2);
        List<Sales> salesList = this.salesMapper.selectList(queryWrapper);
        for (Sales record : salesList) {
            if (null != record.getGoodsid()) {
                QueryWrapper<Inport> queryWrapper1 = new QueryWrapper<>();
                QueryWrapper<Goods> queryWrapper3 = new QueryWrapper<>();
                queryWrapper1.eq(record.getGoodsid() != null, "goodsid", record.getGoodsid());
                Inport inport = this.inportMapper.selectOne(queryWrapper1);
                queryWrapper3.eq(record.getGoodsid() != null, "id", record.getGoodsid());
                Goods goods = this.goodsMapper.selectOne(queryWrapper3);
                if(scrapList.removeAll(salesList)) {
                    queryWrapper2.eq(record.getGoodsid()!=null,"goodsid",record.getGoodsid());
                    Scrap scrap=this.scrapMapper.selectOne(queryWrapper2);
                    if (record.getCustomerid() == 0) {
                        pprofitsum += (record.getPrice() * record.getNumber() - inport.getInportprice() * record.getNumber() - scrap.getNumber() * inport.getInportprice());
                    } else {
                        pprofitsum += (record.getMemberprice() * record.getNumber() - inport.getInportprice() * record.getNumber() - scrap.getNumber() * inport.getInportprice());

                    }
                }
                if(!scrapList.removeAll(salesList)) {
                    if (record.getCustomerid() == 0) {
                        pprofitsum += (record.getPrice() * record.getNumber() - inport.getInportprice() * record.getNumber());
                    } else {
                        pprofitsum += (record.getMemberprice() * record.getNumber() - inport.getInportprice() * record.getNumber());

                    }
                }
                if (pprofitsum > 0) {

                    pprofit.put(goods.getGoodsname(), pprofitsum);
                }
                Map<String, Double> sortedpprofit = new TreeMap<String, Double>(pprofit);
                List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(sortedpprofit.entrySet()); //转换为list
                reportsVo.setPprofitgoods(list);
            }

        }
        return reportsVo.getPprofitgoods();
    }

    @Override
    public  List<Map.Entry<String, Double>> querynprofit(ReportsVo reportsVo) {

        nprofitsum = 0.0;
        MyHashMap<String> nprofit = new MyHashMap<>();
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Scrap> queryWrapper2 = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        queryWrapper2.ge(reportsVo.getStartTime() != null, "scraptime", reportsVo.getStartTime());
        queryWrapper2.le(reportsVo.getEndTime() != null, "scraptime", reportsVo.getEndTime());
        //按时间升序
        queryWrapper.orderByDesc("salestime");
        queryWrapper2.orderByDesc("scraptime");
        List<Scrap> scrapList = this.scrapMapper.selectList(queryWrapper2);
        List<Sales> salesList = this.salesMapper.selectList(queryWrapper);
        for (Sales record : salesList) {
            if (null != record.getGoodsid()) {
                QueryWrapper<Inport> queryWrapper1 = new QueryWrapper<>();
                QueryWrapper<Goods> queryWrapper3 = new QueryWrapper<>();
                queryWrapper1.eq(record.getGoodsid() != null, "goodsid", record.getGoodsid());
                Inport inport = this.inportMapper.selectOne(queryWrapper1);
                queryWrapper3.eq(record.getGoodsid() != null, "id", record.getGoodsid());
                Goods goods = this.goodsMapper.selectOne(queryWrapper3);
                if (scrapList.removeAll(salesList)) {
                    queryWrapper2.eq(record.getGoodsid() != null, "goodsid", record.getGoodsid());
                    Scrap scrap = this.scrapMapper.selectOne(queryWrapper2);
                    if (record.getCustomerid() == 0) {
                        nprofitsum += (record.getPrice() * record.getNumber() - inport.getInportprice() * record.getNumber() - scrap.getNumber() * inport.getInportprice());
                    } else {
                        nprofitsum += (record.getMemberprice() * record.getNumber() - inport.getInportprice() * record.getNumber() - scrap.getNumber() * inport.getInportprice());

                    }
                }
                if (!scrapList.removeAll(salesList)) {
                    if (record.getCustomerid() == 0) {
                        nprofitsum += (record.getPrice() * record.getNumber() - inport.getInportprice() * record.getNumber());
                    } else {
                        nprofitsum += (record.getMemberprice() * record.getNumber() - inport.getInportprice() * record.getNumber());

                    }
                }
                if (nprofitsum < 0) {
                    nprofit.put(goods.getGoodsname(), nprofitsum);
                }
                if (nprofitsum>0)
                {
                    nprofit.put("暂无",0.0);
                }
            }
                    Map<String, Double> sortedpprofit = new TreeMap<String, Double>(nprofit);
                    List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(sortedpprofit.entrySet()); //转换为list
                    reportsVo.setNprofitgoods(list);
                }



                return reportsVo.getNprofitgoods();
        }



}

